import Vue from 'vue'

import App from './App.vue'

import VueRouter from "vue-router";

import ElementUI from 'element-ui';

import 'element-ui/lib/theme-chalk/index.css';

import router from './router/index'

import request from "@/utils/request";

import '@/utils/directvie'

import store from './store/index';

import md5 from "js-md5";

import './assets/icon/iconfont.css'
// import router from "@/router";
Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.use(ElementUI)
Vue.prototype.request = request
Vue.prototype.$md5 = md5;

new Vue({
    render: h => h(App),
    store: store,
    router: router,
    beforeCreate() {
        Vue.prototype.$bus = this
    }
}).$mount('#app')

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    // 目标路由不是登录页，并且还需要token验证，还没有token，那就直接给返回到登录页
    if (to.name !== 'Login' && to.meta.isAuth && !token){
        next({ name: 'loginPage' })
    }else{
        // 目标路由是登录页-自然不需要token验证
        // 或目标路由不需要身份验证
        // 又或目标路由非登录页，需要token验证，但是有token
        // next放行
        next()
    }
})
